package com.gpf.server;

import java.net.ServerSocket;
import java.net.Socket;

public class ZkServerSocket implements Runnable{
    private static int port = 10800;


    public static void main(String[] args) {
        ZkServerSocket zkServerSocket = new ZkServerSocket(port);
        Thread thread = new Thread(zkServerSocket);
        thread.start();
    }

    /**
     * 无参构造函数
      * @param port
     */
    public ZkServerSocket(int port){
        this.port = port;
    }

    public void run() {
        ServerSocket serverSocket = null;
        try{
            serverSocket = new ServerSocket(port);
            System.out.println("Server start port:" + port);
            Socket socket = null;
            while (true){
                // 阻塞式
                socket = serverSocket.accept();
                new Thread(new ServerHandle(socket)).start();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(serverSocket != null) {
                try{
                    serverSocket.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }



}
